<template>
  <div class="mod-config">
    <el-form inline v-show="searchShow" ref="searchForm" :model="dataForm" @keyup.enter.native="searchChange()">
      <el-form-item label="指令名称" prop="code">
        <el-input v-model="dataForm.name" placeholder="指令名称"></el-input>
      </el-form-item>
      <el-form-item label="设备类型" prop="deviceType">
        <el-select v-model="dataForm.deviceList.name" @change="getVendorList" clearable placeholder="选择设备类型">
          <el-option v-for="item in deviceList" :value="item.name" :label="item.name" :key="item.name"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="设备厂商" prop="vendorId">
        <el-select v-model="dataForm.deviceList.from" style="width: 120px;" @change="getTypeList" placeholder="选择设备厂商">
          <el-option v-for="item in vendorList" :value="item.from" :label="item.from" :key="item.from"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="设备型号" prop="typeId">
        <el-select v-model="dataForm.deviceList.type" style="width: 120px;" @change="getDeviceId" placeholder="选择设备型号">
          <el-option v-for="item in typeList" :value="item.type" :label="item.type" :key="item.type"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="searchChange">查询</el-button>
        <el-button type="default" icon="el-icon-delete" @click="searchReset">清空</el-button>
      </el-form-item>

    </el-form>
    <el-row>
      <el-col :span="24">
        <el-button v-if="permissions.pvm_pvmdevicecommand_add" icon="el-icon-plus" type="primary" size="mini" @click="addOrUpdateHandle()">新增</el-button>
        <el-button icon="el-icon-search" circle @click="handleSearchShow" style="float:right;"></el-button>
        <el-button icon="el-icon-refresh" circle @click="refreshChange" style="float:right;"></el-button>
      </el-col>
    </el-row>
    <div class="avue-crud">
      <el-table :data="dataList" border v-loading="dataListLoading">
        <el-table-column prop="deviceId" label="设备id"></el-table-column>
        <el-table-column prop="pvmDevice.name" label="设备类型"></el-table-column>
        <el-table-column prop="pvmDevice.from" label="设备厂商"></el-table-column>
        <el-table-column prop="pvmDevice.type" label="设备型号"></el-table-column>
        <el-table-column prop="name" label="指令名称"></el-table-column>
        <el-table-column prop="command" label="指令"></el-table-column>
        <el-table-column prop="value" label="实际值"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button v-if="permissions.pvm_pvmdevicecommand_edit" type="text" icon="el-icon-edit" @click="addOrUpdateHandle(scope.row.deviceId,scope.row.groupId)">修改</el-button>
            <el-button v-if="permissions.pvm_pvmdevicecommand_del" type="text" icon="el-icon-delete"
              @click="deleteHandle(scope.row.deviceId,scope.row.groupId)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="avue-crud__pagination">
      <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage" background layout="total, sizes, prev, pager, next">
      </el-pagination>
    </div>
    <!-- 弹窗, 新增 / 修改 -->
    <table-form v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></table-form>
  </div>
</template>

<script>
  import { fetchPvmDeviceCommandList, delPvmDeviceCommandObj } from '@/api/pvm/pvmdevicecommand'
  import TableForm from './pvmdevicecommand-form'
  import { mapGetters } from 'vuex'
  import { getDevice, getList } from "@/api/pvm/pvmdevice"
  import { filterParams } from "@/util/tools"
  
  export default {
    data() {
      return {
        dataForm: {
          deviceList: {},
          key: ''
        },
        deviceList: {},
        vendorList: {},
        typeList: {},
        dataList: [],
        searchShow: true,
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        addOrUpdateVisible: false
      }
    },
    components: {
      TableForm
    },
    created() {
      this.getDataList()
      getList("device", this.dataForm).then(response => {
        if(response.data.code == '0'){
          this.deviceList = response.data.data
        }else{
          this.$message.error(response.data.msg)
        }
      })
    },
    computed: {
      ...mapGetters(['permissions'])
    },
    methods: {
      // 获取数据列表
      getDataList() {
        this.dataListLoading = true
        if (this.dataForm.deviceList) {
          this.dataForm.deviceName = this.dataForm.deviceList.name
          this.dataForm.deviceFrom = this.dataForm.deviceList.from
          this.dataForm.deviceType = this.dataForm.deviceList.type
        }
        fetchPvmDeviceCommandList(Object.assign({
          current: this.pageIndex,
          size: this.pageSize
        }, filterParams(this.dataForm))).then(response => {
          if(response.data.code == '0'){
            this.dataList = response.data.data.records
            this.totalPage = response.data.data.total
          }else{
            this.$message.error(response.data.msg)
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 新增 / 修改
      addOrUpdateHandle(id, groupId) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id, groupId)
        })
      },
      // 删除
      deleteHandle(id, groupId) {
        this.$confirm('是否确认删除ID为' + id, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return delPvmDeviceCommandObj(id, groupId)
        }).then(response => {
          if(response.data.code == '0'){
            this.$message.success('删除成功')
            this.getDataList()
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      searchChange() {
        this.pageIndex = 1
        this.getDataList()
      },
      // 搜索清空
      searchReset() {
        this.$refs.searchForm.resetFields()
        this.getDataList(this.page)
      },
      handleSearchShow() {
        this.searchShow = !this.searchShow
      },
      getVendorList() {
        getList("vendor", this.dataForm.deviceList).then(response => {
          if(response.data.code == '0'){
            this.vendorList = response.data.data
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      getTypeList() {
        getList("type", this.dataForm.deviceList).then(response => {
          if(response.data.code == '0'){
            this.typeList = response.data.data
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      getDeviceId() {
        getDevice(this.dataForm.deviceList).then(response => {
          if(response.data.code == '0'){
            this.dataForm.deviceList = response.data.data
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      // 刷新回调
      refreshChange() {
        this.getDataList(this.page)
      }
    }
  }
</script>
